<template>
  <lb-layout
    v-loading="$store.getters.loading"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="#a19f9fb0;"
    :paddBottom="5"
  >
    <aps-form
      :config="apsForm.formConfig"
      :model="apsForm.formData"
      :formRef="apsForm.formRef"
      :labelWidth="apsForm.labelWidth"
      ref="form"
    ></aps-form>
    <div class="title">
      <el-tag type="danger"> 注：节能目标由区域节能主管部门统一设置 </el-tag>
    </div>
    <el-table border height="600" :data="dataList">
      <el-table-column
        prop="areaName"
        label="区域"
        align="center"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        prop="year"
        label="年份"
        align="center"
        show-overflow-tooltip
      >
      </el-table-column>

      <el-table-column
        label="单位面积综合能耗(kgce/m²)"
        align="center"
        show-overflow-tooltip
      >
        <el-table-column
          prop="areaUnitEnergyRangePercent"
          label="下降目标(%)"
          align="center"
          width="250px"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            {{ scope.row.areaUnitEnergyRangePercent * 10000/100 }}
          </template>
        </el-table-column>
      </el-table-column>

      <el-table-column
        label="人均综合能耗(kgce/p)"
        align="center"
        show-overflow-tooltip
      >
        <el-table-column
          prop="personalEnergyRangePercent"
          label="下降目标(%)"
          align="center"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            {{ scope.row.personalEnergyRangePercent * 10000 /100 }}
          </template>
        </el-table-column>
      </el-table-column>

      <el-table-column
        label="人均水耗(t/p)"
        align="center"
        show-overflow-tooltip
      >
        <el-table-column
          prop="personalWaterRangePercent"
          label="下降目标(%)"
          align="center"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            {{ scope.row.personalWaterRangePercent * 10000 /100 }}
          </template>
        </el-table-column>
      </el-table-column>

      <el-table-column
        label="碳排放量(kgCO2)"
        align="center"
        show-overflow-tooltip
      >
        <el-table-column
          prop="personalDcRangePercent"
          label="下降目标(%)"
          align="center"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            {{ scope.row.personalDcRangePercent * 10000 /100 }}
          </template>
        </el-table-column>
      </el-table-column>

      <el-table-column
        prop="createTime"
        label="创建时间"
        align="center"
        show-overflow-tooltip
      >
      </el-table-column>

      <el-table-column label="操作" fixed="right" width="200" align="center">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['energy:target-setting:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="showModel('operate', scope.row)"
          >
            编辑
          </el-button>
          <el-button
            v-hasPermi="['energy:target-setting:remove']"
            class="text-danger"
            type="text"
            icon="el-icon-delete"
            size="small"
            @click="handleOperDelete(scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page-sizes="[20, 40, 60, 80]"
      :page.sync="currentPage"
      :limit.sync="pageSize"
      @pagination="handlerSearchDetail"
      class="mt20"
    />
    <component
      :is="modelName"
      :id="id"
      :visible.sync="show"
      @close="close"
    ></component>
  </lb-layout>
</template>

<script>
import { list, deleteTarget } from "@/api/energyConservation/set.js";
import CascaderSelect from "@/components/Select/CascaderSelect";
import operate from "@/views/work/energyConservation/set/components/operate.vue";
import auth from "@/plugins/auth";
export default {
  name: "energyConservationSet",
  components: {
    operate,
    CascaderSelect,
  },
  dicts: [],
  data() {
    return {
      // areaIdarr: [],
      areaProps: {
        value: "id",
        children: "children",
        label: "label",
        checkStrictly: true,
        expandTrigger: "hover",
      },
      //弹窗
      id: "",
      modelName: "",
      show: false,
      apsForm: {
        formRef: "form",
        labelWidth: "auto",
        formData: {},
        formConfig: {
          formAttr: {
            inline: true,
            size: "small",
          },
          properties: {
            year: {
              label: "年份:",
              typeName: "el-date-picker",
              type: "year",
              size: "small",
              placeholder: "选择年份",
              "value-format": "yyyy",
            },
            areaId: {
              label: "区域:",
              render: (h) => {
                let that = this;
                let valNum = that.$refs?.form?.getFieldsValue().areaId;
                let a = (
                  <CascaderSelect
                    values={valNum}
                    value={valNum}
                    requestMethod={"get"}
                    varient={"areaDept"}
                    propOptions={that.areaProps}
                    filterable={true}
                    size={"small"}
                    placeholder={"请选择区域"}
                    onInput={(value) => {
                      if (value && value.length > 0) {
                        this.$refs?.form?.setFieldValue(
                          "areaId",
                          value[value.length - 1]
                        );
                      } else {
                        this.$refs?.form?.setFieldValue("areaId", null);
                      }
                    }}
                  ></CascaderSelect>
                );
                return <div>{a}</div>;
              },
            },
          },
          formSoltButton: [
            {
              label: "搜索",
              type: "primary",
              typeName: "el-button",
              size: "small",
              icon: "el-icon-search",
              display: true,
              click: this.handlerSearchDetail,
            },
            {
              label: "重置",
              typeName: "el-button",
              size: "small",
              display: true,
              icon: "el-icon-refresh-left",
              click: this.handleReset,
            },
            {
              label: "新增",
              type: "primary",
              typeName: "el-button",
              plain: true,
              display: true,
              size: "small",
              icon: "el-icon-plus",
              click: () => this.showModel("operate"),
            },
          ],
        },
      },
      dataList: [],
      currentPage: 1,
      pageSize: 20,
      total: 0,
    };
  },
  mounted() {
    this.judgePermi();
  },
  methods: {
    judgePermi() {
      // 查询
      if (auth.hasPermi("energy:target-setting:list")) {
        this.apsForm.formConfig.formSoltButton[0].display = false;
        this.apsForm.formConfig.formSoltButton[1].display = false;
        this.handlerSearchDetail();
      }
      // 新增
      if (auth.hasPermi("energy:target-setting:save")) {
        this.apsForm.formConfig.formSoltButton[2].display = false;
      }
    },
    async initData(data) {
      let that = this;
      let query = {
        ...data,
      };
      query.pageNum = that.currentPage;
      query.pageSize = that.pageSize;
      that.$store.commit("app/updateApiCount", "add");
      await list(query)
        .then((res) => {
          that.$store.commit("app/updateApiCount");
          if (res.code === 700) {
            this.$message.warning(res.msg);
            return false;
          }
          that.dataList = res?.rows ?? [];
          that.total = res?.total ?? 0;
        })
        .catch((err) => {
          that.$store.commit("app/updateApiCount");
          // this.$message.error("系统错误,请联系管理员!");
        });
    },
    // 搜索
    handlerSearchDetail() {
      const { ...other } = this.$refs?.form?.getFieldsValue();
      const data = {
        ...other,
      };
      this.initData(data);
    },
    // 重置
    handleReset() {
      this.$refs?.form?.resetFields();
      this.handlerSearchDetail();
    },
    //删除
    handleOperDelete(row) {
      let that = this;
      const text = "是否确认删除" + (row?.name ?? "该条数据") + "？";
      this.$modal
        .confirm(text)
        .then(() => {
          that.$store.commit("app/updateApiCount", "add");
          deleteTarget(row.id)
            .then((res) => {
              that.$store.commit("app/updateApiCount");
              if (res.code === 700) {
                that.$message.warning(res.msg);
                return false;
              }

              that.$message.success("已删除!");
              that.handlerSearchDetail();
            })
            .catch((err) => {
              that.$store.commit("app/updateApiCount");
              // this.$message.error("系统错误,请联系管理员!300405");
            });
        })
        .catch(() => {});
    },
    // 弹窗
    showModel(name, params) {
      this.modelName = name;
      console.log(params);
      this.id = params?.id || "";
      setTimeout(() => {
        this.show = true;
      }, 200);
    },
    close(status) {
      this.show = false;
      setTimeout(() => {
        if (status) this.handlerSearchDetail();
      }, 200);
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .title {
  .el-tag {
    margin-right: 10px !important;
    margin-bottom: 20px;
  }
}
</style>
